Java框架(七)

您所在的位置:网站首页 restful java 框架 Java框架(七)

Java框架(七)

2024-01-16 23:33| 来源: 网络整理| 查看: 265

RESTful开发风格 传统Web应用

在这里插入图片描述

REST与RESTful

REST - 表现层状态转换,资源在网络中以某种表现形式进行状态转换。 RESTful是基于REST理念的一套开发风格,是具体的开发规则。

RESTful传输数据

在这里插入图片描述

RESTful开发规范

使用URL作为用户交互入口。 明确的语义规范(GET | POST | PUT | DELETE)。 只返回数据(JSON | XML),不包含任何展现。

RESTful命名要求

在这里插入图片描述

开发RESTful Web应用

打开IDEA,新建maven web工程,引入SpringMVC依赖 在这里插入图片描述 在web.xml配置SpringMVC

springmvc org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:applicationContext.xml 0 springmvc / characterFilter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 characterFilter /*

在resources目录下创建applicationContext.xml

text/html;charset=utf-8 application/json;charset=utf-8

在com.ql.restful.controller包下创建RestfulController.java类

package com.ql.restful.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("/restful") public class RestfulController { @GetMapping("request") @ResponseBody public String doGetRequest(){ return "{\"message\":\"返回查询结果\"}"; } }

启动项目,在浏览器地址栏中输入http://localhost:8080/restful/request 在这里插入图片描述

实现RESTful实验室

在webapp目录下引入jquery-3.6.0.min.js,并在该目录创建client.html

DOCTYPE html> RESTful实验室 $(function (){ $("#btnGet").click(function (){ $.ajax({ url:"/restful/request", type:"get", dataType:"json", success:function (json){ $("#message").text(json.message); } }) }) $("#btnPost").click(function (){ $.ajax({ url:"/restful/request", type:"post", dataType:"json", success:function (json){ $("#message").text(json.message); } }) }) $("#btnPut").click(function (){ $.ajax({ url:"/restful/request", type:"put", dataType:"json", success:function (json){ $("#message").text(json.message); } }) }) $("#btnDelete").click(function (){ $.ajax({ url:"/restful/request", type:"delete", dataType:"json", success:function (json){ $("#message").text(json.message); } }) }) })

修改RestfulController

package com.ql.restful.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; @Controller @RequestMapping("/restful") public class RestfulController { @GetMapping("request") @ResponseBody public String doGetRequest(){ return "{\"message\":\"返回查询结果\"}"; } @PostMapping("request") @ResponseBody public String doPostRequest(){ return "{\"message\":\"数据新建成功\"}"; } @PutMapping("request") @ResponseBody public String doPutRequest(){ return "{\"message\":\"数据更新成功\"}"; } @DeleteMapping("request") @ResponseBody public String doDeleteRequest(){ return "{\"message\":\"数据删除成功\"}"; } }

运行项目测试 在这里插入图片描述

RestController注解与路径变量

@RestController在Controller类上添加,替换@Controller注解,让每个方法放回json数据,方法上无需再用@ResponseBody注解。 修改RestfulController,使用RestController注解与路径变量

package com.ql.restful.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/restful") public class RestfulController { @GetMapping("request") // @ResponseBody public String doGetRequest(){ return "{\"message\":\"返回查询结果\"}"; } @PostMapping("request/{rid}") // @ResponseBody public String doPostRequest(@PathVariable("rid") Integer requestId){ return "{\"message\":\"数据新建成功\",\"id\":"+requestId+"}"; } @PutMapping("request") // @ResponseBody public String doPutRequest(){ return "{\"message\":\"数据更新成功\"}"; } @DeleteMapping("request") // @ResponseBody public String doDeleteRequest(){ return "{\"message\":\"数据删除成功\"}"; } }

再修改client.html中post请求部分

$("#btnPost").click(function (){ $.ajax({ url:"/restful/request/100", type:"post", dataType:"json", success:function (json){ $("#message").text(json.message+":"+json.id); } }) })

运行测试 在这里插入图片描述



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3